from django.db import models
from django.contrib.auth.models import User,AbstractBaseUser,PermissionsMixin,BaseUserManager
from django.contrib.auth.hashers import make_password
from shortuuidfield import ShortUUIDField
class UserStatusChoices(models.IntegerChoices):
    # 1：激活，2：未激活，3：锁定
    ACTIVE = 1
    UNACTIVE = 2
    LOCKD = 3
class OAUserManager(BaseUserManager):
    use_in_migrations = True
    def _create_user(self, realname, email, password, **extra_fields):
        #创建用户
        if not realname:
            raise ValueError("必须设置真实姓名")
        email = self.normalize_email(email)
        user = self.model(realname=realname, email=email, **extra_fields)
        user.password = make_password(password)
        user.save(using=self._db)
        return user
    def create_user(self, realname, email=None, password=None, **extra_fields):
        #创建普通用户
        extra_fields.setdefault("is_staff", True)
        extra_fields.setdefault("is_superuser", False)
        return self._create_user(realname, email, password, **extra_fields)

    def create_superuser(self, realname, email=None, password=None, **extra_fields):
        #创建超级用户
        extra_fields.setdefault("is_staff", True)
        extra_fields.setdefault("is_superuser", True)
        extra_fields.setdefault("status", UserStatusChoices.ACTIVE)


        if extra_fields.get("is_staff") is not True:
            raise ValueError("超级用户必须设置is_staff=True.")
        if extra_fields.get("is_superuser") is not True:
            raise ValueError("超级用户必须设置is_superuser=True.")

        return self._create_user(realname, email, password, **extra_fields)
#重写User
class OAAbstractUser(AbstractBaseUser, PermissionsMixin):
    realname = models.CharField(max_length=150,unique=False)
    email = models.EmailField(unique=True, blank=False)
    telephone = models.CharField(max_length=20,blank=False)
    is_staff = models.BooleanField(default=False)
    status = models.IntegerField(choices=UserStatusChoices,default=UserStatusChoices.UNACTIVE)
    is_active = models.BooleanField(default=True)
    date_joined = models.DateTimeField(auto_now_add=True)
    department = models.ForeignKey('OADepartment',on_delete=models.SET_NULL,null=True,related_name="staff",related_query_name="staff")
    objects = OAUserManager()

    EMAIL_FIELD = "email"
    USERNAME_FIELD = "email"  # 用电子邮件作为登录字段
    REQUIRED_FIELDS = ["realname","password"]  #必传字段


    def clean(self):
        super().clean()
        self.email = self.__class__.objects.normalize_email(self.email)

    def get_full_name(self):
        return self.realname

    def get_short_name(self):
        return self.realname

#部门表
class OADepartment(models.Model):
    name = models.CharField(max_length=150)
    intro = models.CharField(max_length=150)  # 部门简介
    leader = models.OneToOneField(OAAbstractUser,on_delete=models.SET_NULL,null=True,related_name="department_leader",related_query_name="department_leader")
    manage = models.ForeignKey(OAAbstractUser,on_delete=models.SET_NULL,null=True,related_name="department_manage",related_query_name="department_manage")


